package leetcode.code0498;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution2 implements IDebug {

	public int[] findDiagonalOrder(int[][] mat) {
		int m = mat.length, n = mat[0].length;
		int len = m * n;
		int[] ans = new int[len];
		int x = 0, y = 0;
		boolean d = true;
		for (int i = 0; i < len; i++) {
			ans[i] = mat[x][y];
			if (d) {
				x -= 1;
				y += 1;
				if (x == -1 && y == n) {
					x = 1;
					y = n - 1;
					d = false;
				} else if (x == -1) {
					x = 0;
					d = false;
				} else if (y == n) {
					x += 2;
					y = n - 1;
					d = false;
				}
			} else {
				x += 1;
				y -= 1;
				if (y == -1 && x == m) {
					x = m - 1;
					y = 1;
					d = true;
				} else if (y == -1) {
					y = 0;
					d = true;
				} else if (x == m) {
					y += 2;
					x = m - 1;
					d = true;
				}
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
//		so.debug1();
//		so.debug2();
//		so.debug3();
//		so.debug4();
//		int[] ans = so.findDiagonalOrder(H.str2array1("[[1,2,3],[4,5,6],[7,8,9]]"));
//		H.printObject(ans);
		int[] ans = so.findDiagonalOrder(HelpDebug.str2array1("[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]"));
		HelpDebug.print(ans);

	}

}
